<template>
  <transition
    name="animate__animated animate__bounce"
    appear
    enter-active-class="animate__backInDown"
  >
    <div>
      <el-menu
        class="el-menu-vertical-demo"
        :collapse="isCollapse"
        background-color="#c28b80"
        text-color="#fff"
        active-text-color="#ffd04b"
      >
        <h2>{{ isCollapse ? "About Her" : "About Her系统" }}</h2>
        <!-- 没有子菜单 -->
        <el-menu-item
          :index="item.name"
          v-for="item in noChildren"
          :key="item.name"
          @click="clickMenu(item)"
        >
          <i :class="`el-icon-${item.icon}`"></i>
          <span slot="title">{{ item.label }}</span>
        </el-menu-item>

        <!-- 有子菜单 -->
        <el-submenu
          :index="item.label"
          v-for="item in hasChildren"
          :key="item.label"
        >
          <template slot="title">
            <i :class="`el-icon-${item.icon}`"></i>
            <span slot="title">{{ item.label }}</span>
          </template>
          <el-menu-item-group
            :index="subItem.name"
            v-for="subItem in item.children"
            :key="subItem.name"
          >
            <el-menu-item @click="clickMenu(subItem)" :index="subItem.name">{{
              subItem.label
            }}</el-menu-item>
          </el-menu-item-group>
        </el-submenu>
      </el-menu>
    </div>
  </transition>
</template>

<script>
import { mapState } from "vuex";
import Cookie from "js-cookie";
export default {
  data() {
    return {};
  },
  methods: {
    //点击菜单 跳转
    clickMenu(item) {
      // console.log(item.path);
      // if (
      //   this.$route.path !== item.path &&
      //   !(this.$route.path === "/home" && item.path === "/")
      // ) {
      //   this.$router.push(item.name);
      // }
      this.$router.push(item.path);
      this.$store.commit("tab/selectMenu", item);
    },
  },
  computed: {
    //没有子菜单
    noChildren() {
      return this.menuData.filter((item) => !item.children);
    },

    //有子菜单
    hasChildren() {
      return this.menuData.filter((item) => item.children);
    },

    ...mapState("tab", ["isCollapse"]),

    //获取state menu数据
    menuData() {
      //判断当前数据，如果缓存中没有，那么从当前的store中去获取
      return JSON.parse(Cookie.get("menu")) || this.$store.state.tab.menu;
    },

    // checkedNav() {
    //   // console.log(this.$router);
    //   return JSON.parse(Cookie.get("menu")) || this.$store.state.tab.menu;
    // },
  },
  mounted() {
    console.log(this.$router);
  },
};
</script>

<style scoped lang="less">
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 791px;
}

.el-menu {
  height: 100vh;
  border-right: none;
  h2 {
    text-align: center;
    font-family: "微软雅黑";
    color: aliceblue;
    line-height: 48px;
    font-size: 16px;
    font-weight: 400;
  }
}
</style>
